###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/SearchResources.pm.pl -> SearchResources module         #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::SearchResources;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";
require "Tables.pm.pl";

use HTML::Dialog;
use HTML::Form;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();
  my $Form   = HTML::Form->new();

  my $Body  = $self->myRegExpField(name => "FORM_TITLE", subject => "Title");
     $Body .= $self->myRegExpField(name => "FORM_URL", subject => "URL");
     $Body .= $self->myRegExpField(name => "FORM_DESCRIPTION", subject => "Description");
     $Body .= $self->myRegExpField(name => "FORM_KEYWORDS", subject => "Keywords");
     $Body .= $self->myRegExpField(name => "FORM_CONTACT_NAME", subject => "Contact Name");
     $Body .= $self->myRegExpField(name => "FORM_CONTACT_EMAIL", subject => "Contact Email");
  
  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_CATEGORY",
    subject   => "Category",
    multiple  => 1,
    rows      => 5,
    items     => [&Standard::MapArray({ value => "\${ID}", label => "\${FULL_TITLE}" }, $in{'input'}->{'CATEGORIES'})]
  );
  
  $Body .= $Dialog->FormTextBox(
    value     => $Form->SelectBox(name => "FORM_CREATE_TYPE", class => "", items => [{ value => "MORE", label => "more" }, { value => "LESS", label => "less" }])." than ".$Form->TextBox(name => "FORM_CREATE", class => "")." days ago.",
    subject   => "Created"
  );

  $Body .= $Dialog->FormTextBox(
    value     => $Form->SelectBox(name => "FORM_UPDATE_TYPE", class => "", items => [{ value => "MORE", label => "more" }, { value => "LESS", label => "less" }])." than ".$Form->TextBox(name => "FORM_UPDATE", class => "")." days ago.",
    subject   => "Updated"
  );

  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_STATUS",
    subject   => "Status",
    multiple  => 1,
    rows      => 2,
    items     => [
      { value => "0", label => "Inactive" },
      { value => "1", label => "Active" }
    ]
  );

  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_FLAGS",
    subject   => "Flags",
    multiple  => 1,
    rows      => 3,
    items     => [
      { value => "MODIFIED", label => "Modified" },
      { value => "DEAD_LINK", label => "Dead Link" },
      { value => "DELETED", label => "Deleted" }
    ]
  );
  
  $Body .= $Dialog->FormRadio(
    name      => "FORM_TYPE",
    value     => "NORMAL",
    subject   => "Search Type",
    items     => [
      { value => "NORMAL", label => "Normal" },
      { value => "CHECK", label => "Check, timeout: ".$Form->TextBox(name => "FORM_TIMEOUT", value => "10", class => "", extra => { size => "3" })." seconds." }
    ], join => "<br>"
  );

  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Search" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Search Resources",
    form    => 1,
    hiddens => [
      { name => "action", value => "DoSearchResources" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );
  
  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = ("input" => {}, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my @Fields = (
    {
      title     => "Sel",
      width     => "1",
      template  => sub { my ($s, $d, $f, $r) = @_;
        return { body => $f->CheckBox(name => "RID", items => [{ value => $r->{'ID'} }]) };
      }
    },
    {
      title     => "ID",
      width     => "45",
      sort      => "ID",
      nowrap    => 1,
      template  => sub { my ($s, $d, $f, $r) = @_;
        return { body => $r->{'ID'} };
      }
    },
    {
      title     => "Title",
      width     => "65%",
      sort      => "TITLE",
      main      => 1,
      template  => sub{ my ($s, $d, $f, $r) = @_;
        if ($SL::QUERY{'FORM_TYPE'} eq "CHECK") {
          return { body => ($r->{'STATUS'} ? "<img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Status/active.gif\" alt=\"Active\">" : "<img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Status/inactive.gif\" alt=\"Inactive\">")." <b><a href=\"$SYSTEM->{'SCRIPT_URL'}?CP=1&action=ModifyResource&RID=$r->{'ID'}&Referer=".$SL::CGI->escape($s->URL())."&SID=$SL::ADMIN{'SID'}\">".$r->{'TITLE'}."</a></b><br>".$r->{'DESCRIPTION'}."<br>Category: ".$r->{'category'}->{'FULL_TITLE'}."<br>Check: ".($r->{'check'}->{'PASS'} ? "<font color=\"Green\">OK ($r->{'check'}->{'CODE'})</font>" : "<font color=\"Red\">Fail ($r->{'check'}->{'CODE'}) $r->{'check'}->{'MESSAGE'}") };
        } else {
          return { body => ($r->{'STATUS'} ? "<img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Status/active.gif\" alt=\"Active\">" : "<img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Status/inactive.gif\" alt=\"Inactive\">")." <b><a href=\"$SYSTEM->{'SCRIPT_URL'}?CP=1&action=ModifyResource&RID=$r->{'ID'}&Referer=".$SL::CGI->escape($s->URL())."&SID=$SL::ADMIN{'SID'}\">".$r->{'TITLE'}."</a></b><br>".$r->{'DESCRIPTION'}."<br>Category: ".$r->{'category'}->{'FULL_TITLE'} };
        }
      }
    },
    {
      title     => "Flags",
      width     => "60",
      nowrap    => 1,
      template  => sub { my ($s, $d, $f, $r) = @_;
        my $flags = &Standard::ArrayToHash([ split(/,/, $r->{'FLAGS'}) ]);
        my $return;
        if ($flags->{'MODIFIED'}) {
          $return .= " <img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Flags/modified.gif\" alt=\"Modified\">";
        }
        if ($flags->{'DEAD_LINK'}) {
          $return .= " <img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Flags/deadlink.gif\" alt=\"Dead Link\">";
        }
        if ($flags->{'DELETED'}) {
          $return .= " <img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Flags/deleted.gif\" alt=\"Deleted\">";
        }
        if ($flags->{'DUPLICATE'}) {
          $return .= " <img border=\"0\" src=\"$SYSTEM->{'PUBLIC_URL'}/Images/Flags/duplicate.gif\" alt=\"Duplicate URL\">";
        }
        return { body => $return };
      }
    },
    {
      title     => "Contact",
      width     => "150",
      nowrap    => 1,
      sort      => "CONTACT_NAME",
      template  => sub { my ($s, $d, $f, $r) = @_;
        return { body => "<a href=\"mailto:$r->{'CONTACT_EMAIL'}\">$r->{'CONTACT_NAME'}</a>" };
      }
    },
    {
      title     => "Created",
      width     => "100",
      nowrap    => 1,
      sort      => "CREATE_TIME",
      template  => sub { my ($s, $d, $f, $r) = @_;
        return { body => "$r->{'CREATE_DATE'} at $r->{'CREATE_TIME'}" };
      }
    },
    {
      title     => "",
      width     => "35%",
      template  => sub { my ($s, $d, $f, $r) = @_;
        return { body => $d->TableToolbar(
          items => [
            { type => "divider" },
            { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?action=Detail&RID=$r->{'ID'}", target => "_blank", icon => "Toolbar/view.gif", caption => "View" },
            { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=ModifyResource&RID=$r->{'ID'}&Referer=".$SL::CGI->escape($s->URL())."&SID=$SL::ADMIN{'SID'}", icon => "Toolbar/modify.gif", caption => "Modify" },
            { type => "icon", url => "javascript:ConfirmRemove('$SYSTEM->{'SCRIPT_URL'}?CP=1&action=DoRemoveResources&RID=$r->{'ID'}&Referer=".$SL::CGI->escape($SL::CGI->escape($s->URL()))."&SID=$SL::ADMIN{'SID'}');", icon => "Toolbar/remove.gif", caption => "Remove" },
            { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Reviews&RID=$r->{'ID'}&SID=$SL::ADMIN{'SID'}", icon => "Toolbar/reviews.gif", caption => "Reviews" }
          ]
        ) };
      }
    }
  );    

  foreach my $resource (@{ $in{'input'}->{'RESOURCES'} }) {
    $resource->{'category'} = $in{'input'}->{'CATEGORIES'}->[$in{'input'}->{'CATEGORIES_IX'}->{$resource->{'CATEGORY'}}];
  }

  my $Tables = Tables->new();

  return $Tables->Build(
    header      => "Search Resources",
    categories  => $in{'input'}->{'CATEGORIES'},
    records     => $in{'input'}->{'RESOURCES'},
    pages       => $in{'input'}->{'PAGES'},
    fields      => \@Fields,
    form_extra  => { onSubmit => "return ConfirmRemove();" },
    del_field   => "RID",
    hiddens     => [
      { name => "Referer", value => $Tables->URL() },
      { name => "action", value => "DoRemoveResources" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ],
    toolbar     => [
      { type => "divider" },
      { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=CreateResource&FORM_CATEGORY=$SL::QUERY{'Root'}&Referer=".$SL::CGI->escape($Tables->URL())."&SID=$SL::ADMIN{'SID'}", icon => "Toolbar/new.gif", caption => "New" },
      { type => "icon", url => "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=SearchResources&SID=$SL::ADMIN{'SID'}", icon => "Toolbar/search.gif", caption => "Search" }
    ],
    buttons     => [
      { type => "button", value => "Select All", extra => { onClick => "CheckAll(form.RID, ".scalar(@{ $in{'input'}->{'RESOURCES'} }).")" } },
      { type => "button", value => "Delete All Selected", extra => { onClick => "if(ConfirmRemove()){form.action.value='DoRemoveResources';form.submit();}" } },
      { type => "button", value => "Approve Selected", extra => { onClick => "form.action.value='ApproveResources';form.submit();" } },
      { type => "reset", value => "Cancel" }
    ]
  );
}

###############################################################################
# myRegExpField subroutine
sub myRegExpField {
  my $self = shift;
  my %in = ("name" => "", "subject" => "", @_);

  my $Dialog = HTML::Dialog->new();
  my $Form   = HTML::Form->new();

  my $Value  = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
     $Value .= "<tr><td width=\"75\" nowrap>";
     $Value .= $Form->SelectBox(name => $in{'name'}."_TYPE", items => [{ value => "IS", label => "is" }, { value => "CONTAINS", label => "contains" }]);
     $Value .= "</td><td width=\"5\" nowrap></td><td width=\"100%\">";
     $Value .= $Form->TextBox(name => $in{'name'});
     $Value .= "</td></tr></table>";
  
  return $Dialog->FormTextBox(
    value   => $Value,
    subject => $in{'subject'}
  );
}

1;